// 用固定数组实现前缀树，空间使用是静态的。推荐！
// 测试链接 : https://www.nowcoder.com/practice/7f8a8553ddbf4eaab749ec988726702b
// 提交以下的全部代码，可以直接通过

#include <cstdio>
#include <string>
#include <iostream>
#include <cstring>

using namespace std;

// 如果将来增加了数据量，就改大这个值
const int MAXN = 150001;

int tree[MAXN][26];
int end[MAXN];
int pass[MAXN];

int cnt;

void Build()
{
    cnt = 1;
}

void Insert(string& word)
{
    int cur = 1;
    ++pass[cur];
    for(int i = 0, path; i < word.size(); ++i)
    {
        path = word[i] - 'a';
        if(tree[cur][path] == 0)
        {
            tree[cur][path] = ++cnt;
        }
        cur = tree[cur][path];
        ++pass[cur];
    }
    ++::end[cur];
}

int Search(string& word)
{
    int cur = 1;
    for(int i = 0, path; i < word.size(); ++i)
    {
        path = word[i] - 'a';
        if(tree[cur][path] == 0) return 0;
        cur = tree[cur][path];
    }
    return ::end[cur];
}

int prefixNumber(string& word)
{
    int cur = 1;
    for(int i = 0, path; i < word.size(); ++i)
    {
        path = word[i] - 'a';
        if(tree[cur][path] == 0) return 0;
        cur = tree[cur][path];
    }
    return pass[cur];
}

void Delete(string& word)
{
    if(Search(word) > 0)
    {
        int cur = 1;
        for(int i = 0, path; i < word.size(); ++i)
        {
            path = word[i] - 'a';
            if(--pass[tree[cur][path]] == 0)
            {
                tree[cur][path] = 0;
                return;
            }
            cur = tree[cur][path];
        }
        --::end[cur];
    }
}

void Clear()
{
    for(int i = 1; i <= cnt; ++i)
    {
        memset(tree[i], 0, sizeof(tree[i]));
        ::end[i] = 0;
        pass[i] = 0;
    }
}

int main()
{
    int m, op;
    string word;
    scanf("%d", &m);
    Build();
    for(int i = 0; i < m; ++i)
    {
        cin >> op >> word;
        if(op == 1) Insert(word);
        else if(op == 2) Delete(word);
        else if(op == 3)
        {
            if(Search(word)) printf("YES\n");
            else printf("NO\n");
        }
        else if(op == 4) printf("%d\n", prefixNumber(word));
    }
    Clear();

    return 0;
}